package com.dek.ssm.ch16.advice;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

@ControllerAdvice(basePackages= {"com.dek.ssm.chl6.controller"})
public class CommonControllerAdvice {
	
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		//针对日期类型的格式化，其中 CustomDateEditor 是客户自定义编辑器
		//它的 boolean  参数表示是否允许为空
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}
	
	@ModelAttribute
	public void populateModel(Model model) {
		model.addAttribute("name", "dek");
	}
	
	@ExceptionHandler(Exception.class)
	public String exception() {
		return "error";
	}
	
	
}
